package com.fruits.service.impl;

import com.fruits.domain.User;
import com.fruits.dao.UserDao;
import com.fruits.dao.impl.UserDapImpl;
import com.fruits.service.UserService;

import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao=new UserDapImpl();
    @Override
    public User add(User user)  {
        int add=userDao.add(user);
        if(add==1){
            User loginUser = login(user.getEmail(), user.getPwd());
            return loginUser;
        }
        return null;
    }

    @Override
    public User login(String str, String pwd)  {
        User user = userDao.findByStr(str);
        if(user.getPwd().equals(pwd)){
            user.setPwd("******");
            return user;
        }

        return null;
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    public boolean update(User user) {
     int update=   userDao.update(user);
     if(update==1){

         return true;
     }
     return false;
    }

    @Override
    public boolean del(int id) {
        int del = userDao.delete(id);
        if(del==1){
            return true;
        }
        return false;
    }

    @Override
    public User findById(int uid) {
       return userDao.findById(uid);
    }
}
